package roslan.musicbox.utils;

import java.io.File;
import java.util.Scanner;
import java.util.Vector;

public class SourceCodeStatistics {
	private Vector<StringBuffer> lineInfo;
	public SourceCodeStatistics(){
		lineInfo = new Vector<StringBuffer>();
		lineInfo.clear();
	}
	public int countLines(File path, int depth){
		if(path.isDirectory()){
			StringBuffer buffer = new StringBuffer();
			for(int i=0;i<depth;++i){
				buffer.append("  ");
			}
			buffer.append(path.getName()+"-------");
			int index = lineInfo.size();
			lineInfo.add(buffer);
			int count = 0;
			File[] fileList = path.listFiles();
			for(File file : fileList){
				count += countLines(file, depth+1);
			}
			lineInfo.get(index).append(count);
			return count;
		}else if(path.isFile()){
			Scanner scanner = null;
			try{
				scanner = new Scanner(path);
			}catch(Exception e){}
			int count = 0;
			while(scanner.hasNextLine()){
				++count;
				scanner.nextLine();
			}
			StringBuffer buffer = new StringBuffer();
			for(int i=0;i<depth;++i){
				buffer.append("  ");
			}
			buffer.append(path.getName()+"-------"+count);
			lineInfo.add(buffer);
			return count;
		}
		return 0;
	}
	public void output(){
		for(StringBuffer buffer : lineInfo){
			System.out.println(buffer);
		}
	}
	public static void main(String[] args){
		SourceCodeStatistics statistics = new SourceCodeStatistics();
		File file = new File("src");
		statistics.countLines(file, 0);
		statistics.output();
	}	                                       
}
